package com.david.system.mapper;

import com.david.system.pojo.DTO.UserSearchDTO;
import com.david.system.pojo.Entity.*;
import com.david.system.pojo.VO.MenuVO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Mapper
public interface UserMapper {

    @Select("select id, email, username, password, create_time, enabled, name, update_time, gender, code, phone, status, dept_id, job_id from sys_user where username = #{username}")
    UserEntity selectUserByUsername(String username);


    List<UserInfo> selectUserAll();

    UserInfo getUserInfo(String username);

    List<UserInfo> selectUserByDTO(UserSearchDTO userSearchDTO);

    @Select("select id, name, code, parent_id from sys_dept")
    List<DeptEntity> selectDeptAll();

    @Select("select id, name, dept_id, description, code from sys_job")
    List<JobEntity> selectJobAll();

    @Select("select id, role_code, role_name, create_time, description, update_time from sys_role")
    List<RoleEntity> selectRoleAll();


    void updateUser(UserEntity userEntity);

    @Select("select id from sys_dept where name = #{dept}")
    Integer selectDepIdByName(String dept);

    @Select("select id from sys_job where name = #{job}")
    Integer selectJobIdByName(String job);

    @Select("select id from sys_job where name = #{name} and dept_id = #{deptId}")
    Integer selectJobIdByNameDeptid(@Param("name") String name, @Param("deptId") Integer deptId);

    @Select("select id from sys_role where role_name = #{role}")
    Integer selectRoleIdByName(String role);

    Long insertUser(@Param("userEntity") UserEntity userEntity);

    @Insert("insert into sys_role_user (user_id, role_id, create_time, update_time) VALUES (#{userId}, #{roleId}, #{now}, #{now})")
    void insertRoleUser(@Param("userId") Long userId, @Param("roleId") Integer roleId,@Param("now") LocalDateTime now);

    @Delete("delete from sys_user where id = #{id}")
    void delUser(Long id);

    @Update("update  sys_role_user set role_id = #{roleId}, update_time = #{now} where user_id = #{userId}")
    void updateRoleUser(@Param("userId") Long userId, @Param("roleId") Integer roleId,@Param("now") LocalDateTime now);

    @Select("select id, name, dept_id, description, code from sys_job where dept_id = #{deptId}")
    List<JobEntity> selectJobByDeptId(Integer deptId);

    List<JobEntity> selectJobByIds(List<Integer> ids);

    @Select("select id, name, parent_id, path, type, sort, component, permission, icon from sys_menu")
    List<MenuVO> selectMenuAll();


    List<Integer> selectMenuByRoleId(@Param("id") Integer id);

    void insertRoleMenuId(@Param("roleId") Integer roleId, @Param("ids") List<Integer> ids, @Param("now") LocalDateTime now);

    @Delete("delete from sys_role_menu where role_id = #{roleId}")
    void deleteRoleMenu(Integer roleId);

    @Delete("delete from sys_role where id = #{id}")
    void deleteRoleById(Integer id);

    List<RoleEntity> selectRole(@Param("roleName") String roleName,@Param("roleCode") String roleCode);

//    @Insert("insert into sys_role (role_code, role_name, create_time, description, update_time) values (#{roleCode}, #{roleName},#{createTime},#{description},#{updateTime})")
    @Insert("insert into sys_role (role_code, role_name, create_time, description, update_time) values (#{role.roleCode}, #{role.roleName},#{role.createTime},#{role.description},#{role.updateTime})")
    void inserRole(@Param("role") RoleEntity role);

    @Update("update sys_dept set name = #{deptName} where id = #{deptId}")
    void updateDeptName(@Param("deptId") Integer deptId, @Param("deptName") String deptName);


    List<DeptEntity> selectDepByNameParentid(@Param("name") String name,@Param("parentId") Integer parentId);

    @Insert("insert into sys_dept (name, parent_id) values (#{name},#{parentId})")
    void insertDept(@Param("name") String name,@Param("parentId") Integer parentId);

    @Delete("delete from sys_dept where id = #{id}")
    void deleteDept(Integer id);

   @Insert("insert into sys_job (name, dept_id, description, code) VALUES (#{job.name},#{job.deptId},#{job.description},#{job.code})")
    void insertJob(@Param("job") JobEntity job);

    void deleteJobsByIds(@Param("ids") List<Integer> ids);

    List<String> selectMenuPermByRoleId(@Param("roleId") Integer roleId, @Param("btnPermission") List<String> btnPermission);


    List<String> selectMenuPageByRoleId(@Param("roleId") Integer roleId, @Param("pagePermission") List<String> pagePermission);

    List<MenuEntity> selectMenuByPath(@Param("pathList") ArrayList<String> pathList);

    List<UserInfo> selectUserByIds(@Param("adminIds") List<Long> adminIds);

    @Select("select id, name, code, parent_id from sys_dept where id = #{deptId}")
    DeptEntity selectDepById(Integer deptId);
}
